package com.pixiv.MysqlDAO;

import com.pixiv.Interface.RegisterPicture;
import com.pixiv.Interface.UserLogin;
import com.pixiv.user.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.DigestUtils;


import static com.pixiv.MysqlDAO.CommonDAO.Factory;


public class UserLoginDAO{
    public static SqlSessionFactory getFactory()
    {

        return Factory;
    }
    public static User getUserByLoginParameter(String name, String pwd)
    {
        SqlSession s = getFactory().openSession();
        UserLogin UL = s.getMapper(UserLogin.class);

        String pwdMd5 = DigestUtils.md5DigestAsHex(pwd.getBytes());

        return UL.getUserByLoginParameter(name,pwdMd5);
    }
    public static User getUserByLoginParameter(String name)
    {
        SqlSession s = getFactory().openSession();
        UserLogin UL = s.getMapper(UserLogin.class);
        return UL.getUserByLoginParameterOnlyName(name);
    }

    public static void RegisterUserByLoginParameter(String name,String pwd)
    {
        SqlSession s = getFactory().openSession();
        UserLogin UL = s.getMapper(UserLogin.class);

        String pwdMd5 = DigestUtils.md5DigestAsHex(pwd.getBytes());

        UL.AddUserWithLoginParameter(name,pwdMd5);
        s.commit();
    }

    public static User initLoginUserData(User u)
    {
        return u;
    }

}
